<?php
	// check number of params and their names
	if (sizeof($_POST) != 4 || !array_key_exists('un', $_POST) || !array_key_exists('is2fa', $_POST) || !array_key_exists('otptype', $_POST) || !array_key_exists('isia', $_POST))
	{
		echo 1;
		exit();
	}

	// if passed then connect to db and define variables
	$mysqli = new mysqli('localhost', 'root', 'project3', 'server2');
	if ($mysqli->connect_errno)
	{
		echo 2;
		exit();
	}
	$un = $_POST['un'];
	$is2fa = ($_POST['is2fa'] == "true" ? true : false);
	$otptype = $_POST['otptype'];
	$isia = ($_POST['isia'] == "true" ? true : false);

	// if passed then try to save the settings
	$res = $mysqli->query("select count(*) as total from users where un like '$un'");
	$ob = $res->fetch_object();
	if ($ob->total != 1)
	{
		echo 3;
		$mysqli->close();
		exit();
	}

		// check otp type validity
	$res = $mysqli->query("select * from users where un like '$un'");
	$ob = $res->fetch_object();
	if ($otptype == 'p')
	{
		if ($ob->phone == null)
		{
			echo "phone number is needed to enable 2FA by SMS!";
			$mysqli->close();
			exit;
		}
	}
	if ($otptype == 'e')
	{
		if ($ob->email == null)
		{
			echo "email address is needed to enable 2FA by email!";
			$mysqli->close();
			exit;
		}
	}

		// if isia then check if is2fa is enable
	if ($isia)
	{
		if ($ob->is2fa == 'n')
		{
			echo "please enable 2FA first before enabling Implicit Authentication!";
			$mysqli->close();
			exit;
		}

			// if passed then check if an ia account has been added or not, if not then try to add
		if ($ob->isiaaccount == 'n')
		{
			$res1 = $mysqli->query("select * from ia_cred");
			$ob1 = $res1->fetch_object();
			$res1->close();

			do
			{
				$url = ("http://173.1.2.194/addAccount.php?");
				$url .= "id=" . $ob1->id;
				$url .= "&key=" . $ob1->secretkey;
				$url .= "&hun=" . sha1($un);
				$code = sha1($un . time() . rand());
				$url .= "&code=" . $code;
				$ch = curl_init($url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$result = curl_exec($ch);
				curl_close($ch);

			} while ($result == '1');
			if ($result != '0')
			{
				echo $result;
				$mysqli->close();
				exit;
			}
				
				// update isiaaccount
				$mysqli->query("update users set isiaaccount = 'y' where un like '$un'");

				// email the user the code
				mailCode($code, $ob->email);
		}
	}


	$sql = "update users set otppref = " . ($otptype == 'e' ? "'e'" : "'p'") . ", is2fa = " . ($is2fa ? "'y'" : "'n'") . ", isia = " . ($isia ? "'y'" : "'n'") . " where un like '$un'";
	$mysqli->query($sql);
	if ($mysqli->affected_rows != 1)
	{
		echo 4;
		$mysqli->close();
		exit();
	}

	echo 0;
	$mysqli->close;
	exit();
	

	function mailCode($code, $email)
	{
		require_once "Mail.php";

		$from = "Server-2";
		$to = "$email";
		$subject = "Your Implicit Authentication activation code";
		$body = "Please copy and paste the code below into the IA app on your smartphone to enable Implicit Authentication on our website. <br /><br />$code";
		$host = "ssl://smtp.gmail.com";
		$port = "465";
		$username = "cs249project3";  
		$password = "project3";

		$headers = array ('From' => $from,
			'To' => $to,
			'Subject' => $subject,
			'Content-type' => "text/html");
			$smtp = Mail::factory('smtp',
			array ('host' => $host,
			'port' => $port,
			'auth' => true,
			'username' => $username,
			'password' => $password));

		$mail = $smtp->send($to, $headers, $body);

		if (PEAR::isError($mail)) 
		{
			echo "The server experienced error while sending you an email. Please try again later. \n", $mail->getMessage();  
			exit();
		}
	}
?>
